import React, {Component} from 'react';
// 引入消息发布订阅的库
import PubSub from "pubsub-js"
import "./index.css"

class List extends Component {
    state = {
        users:[], // 初始化状态user
        isFirst:true,// 标识是否为第一次打开界面
        isLoading:false, // 标识请求之前默认为false
        err:''// 标识存储相关的错误信息
    }
    // 组件完成渲染
    componentDidMount() {
        PubSub.subscribe('message',(msg,stateObj)=>{
            console.log("收到的数据",stateObj)
            console.log("收到的消息名",msg)
            this.setState(stateObj)
        })

    }

    render() {
        const {users,isFirst,isLoading,err} = this.state

        // console.log('users',users)
        return (
            <div className="row">
                {
                    isFirst?<h2>输入关键字，点击搜索</h2>:
                    isLoading?<h2>Loading....</h2>:
                    err?<h2 style={{background:'red'}}>{err}</h2>:
                    users.map(userObj=>{
                        return (
                            <div className="card" key={userObj.id}>
                                <a rel="noreferrer" href={userObj.html_url} target="_blank">
                                    <img alt="avatar" src={userObj.avatar_url} style={{width:'100px'}}/>
                                </a>
                                <p className="card-text">{userObj.login}</p>
                            </div>
                        )
                    })
                }
            </div>
        );
    }
}

export default List;
